import { getToken } from "./auth";
const imgBaseUrl = 'https://oss.yukukeji.com/';
import baseURL from './baseURL';
let fileUpload = filePath => {
	uni.showLoading({
		title: "图片处理中",
		mask: true
	})
	const lastIndex = filePath.lastIndexOf('.');
	let filetype = '';
	if (lastIndex !== -1) {
		filetype = filePath.substring(lastIndex + 1);
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: baseURL + '/file/uploadFile',
			filePath,
			header: {
				'Content-Type': 'multipart/form-data',
				token: getToken(),
			},
			formData: {
				// 该字段是后端除了需要文件之外的其他参数
				fileType: 'image/' + filetype
			},
			// name的值为后端接口与文件相对于的字段名
			name: 'multipartFile',
			success(res) {
				if (res.statusCode == 200) {
					const fileObj = JSON.parse(res.data).data;
					let onlineFilePath = imgBaseUrl + fileObj.filePath + fileObj.fileNewName;
					uni.hideLoading();
					resolve({code:200,data:onlineFilePath,message:"操作成功"});
				} else {
					uni.hideLoading();
					uni.utils.toast("图片上传失败");
					reject(res)
				}
			},
			fail(f) {
				console.log('f', f);
				uni.hideLoading();
				reject(f)
			},
		})
	})
}
export default fileUpload